package com.OID.test;

import com.OID.model.HiloTester;
import com.OID.model.IdentityTester;
import com.OID.model.IncrementTester;
import com.OID.model.NativeTester;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import java.util.Iterator;
import java.util.List;

public class BusinessService {
    private static SessionFactory sessionFactory;
    static {
        try {
            Configuration config = new Configuration().configure();
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
            sessionFactory = config.buildSessionFactory(serviceRegistry);
        } catch (RuntimeException e) {
            System.out.println("");
            e.printStackTrace();

        }
    }

    private void findAllObjects(String className) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            List objects = session.createQuery("from " + className).list();
            for (Iterator it = objects.iterator(); it.hasNext(); ) {
                Long id = 0L;
                if (className.equals("mypack.NativeTester")) id = ((NativeTester) it.next()).getId();
                if (className.equals("mypack.IncrementTester")) id = ((IncrementTester) it.next()).getId();
                if (className.equals("mypack.IdentityTester")) id = ((IdentityTester) it.next()).getId();
                if (className.equals("mypack.HiloTester")) id = ((HiloTester) it.next()).getId();
                System.out.println("ID of " + className + ":" + id);
            }
            tx.commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }
    private void saveObject(Object object) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(object);
            tx.commit();
            System.out.println();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void deleteAllObjects(String className) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("delete from " + className);
            query.executeUpdate();
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
        } finally {
            session.close();
        }
    }

    private void test(String className) throws Exception {
        deleteAllObjects(className);
        Object o1 = Class.forName(className).newInstance();
        saveObject(o1);
        Object o2 = Class.forName(className).newInstance();
        saveObject(o2);
        Object o3 = Class.forName(className).newInstance();
        saveObject(o3);
        findAllObjects(className);

    }

    public static void main(String args[]) throws Exception {
        String className;
        if (args.length == 0) {
            className = "com.OID.model.NativeTester";
        } else {
            className = args[0];
        }
        new BusinessService().test(className);

        sessionFactory.close();
    }
}

